package com.blrs.service.enterprise.person.impl;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

import com.blrs.common.annotation.DataScope;
import com.blrs.common.exception.ServiceException;
import com.blrs.common.utils.ReflectionUtils;
import com.blrs.common.utils.SecurityUtils;
import com.blrs.common.utils.uuid.UUID;
import com.blrs.mapper.EnterpriseInfoMapper;
import com.blrs.mapper.QualificationInfoMapper;
import com.blrs.service.enterprise.person.IEnterprisePersonInfoService;
import com.blrs.system.mapper.SysUploadFileMapper;
import com.blrs.system.mapper.SysUploadMapper;
import com.blrs.system.mapper.SysUserMapper;
import com.blrs.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.blrs.mapper.EnterprisePersonInfoMapper;
import com.blrs.domain.EnterprisePersonInfo;
import org.springframework.transaction.annotation.Transactional;

/**
 * 企业人员管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-07-19
 */
@Service
public class EnterprisePersonInfoServiceImpl implements IEnterprisePersonInfoService
{
    @Autowired
    private EnterprisePersonInfoMapper enterprisePersonInfoMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysUploadFileMapper sysUploadFileMapper;
    @Autowired
    private EnterpriseInfoMapper enterpriseInfoMapper;
    @Autowired
    private ISysDeptService sysDeptService;
    /**
     * 查询企业人员管理
     * 
     * @param id 企业人员管理主键
     * @return 企业人员管理
     */
    @Override
    public EnterprisePersonInfo selectEnterprisePersonInfoById(String id)
    {
        EnterprisePersonInfo enterprisePersonInfo = enterprisePersonInfoMapper.selectEnterprisePersonInfoById(id);
        List<String> list = enterpriseInfoMapper.selectIsPersonByName(enterprisePersonInfo.getEnterpriseId());
        if (list.size()>0){
            enterprisePersonInfo.setEnterpriseName(list.get(0));
        }
        enterprisePersonInfo.setFileUrl(sysUploadFileMapper.selectUrl(enterprisePersonInfo.getUploadFile()));
        return enterprisePersonInfo;
    }

    /**
     * 查询企业人员管理列表
     * 
     * @param enterprisePersonInfo 企业人员管理
     * @return 企业人员管理
     */
    @Override
    public List<EnterprisePersonInfo> selectEnterprisePersonInfoList(EnterprisePersonInfo enterprisePersonInfo)
    {

        List<EnterprisePersonInfo> enterprisePersonInfos = enterprisePersonInfoMapper.selectEnterprisePersonInfoList(enterprisePersonInfo);
        enterprisePersonInfos.stream()
                .forEach(personInfo ->
                        {
//                            personInfo.setFileUrl(sysUploadFileMapper.selectUrlByCode("enterprise_person",personInfo.getUploadFile()));
                            List<String> list = enterpriseInfoMapper.selectIsPersonByName(personInfo.getEnterpriseId());
                            if (list.size()>0){
                                personInfo.setEnterpriseName(list.get(0));
                            }
                        }
                );
        return enterprisePersonInfos;
    }

    /**
     * 新增企业人员管理
     * 
     * @param enterprisePersonInfo 企业人员管理
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertEnterprisePersonInfo(EnterprisePersonInfo enterprisePersonInfo) {
        enterprisePersonInfo.setOrganizationId(SecurityUtils.getOrgId());
        List<EnterprisePersonInfo> enterprisePersonInfos = enterprisePersonInfoMapper.selectEnterprisePersonInfoListByPhone(enterprisePersonInfo);
        if (enterprisePersonInfos.size()>0){
            throw new ServiceException("电话号码不可重复");
        }
        try {
            ReflectionUtils.setCommonPrivateProperty(enterprisePersonInfo);
            enterprisePersonInfo.setCreationTime(new Timestamp(new Date().getTime()));
            enterprisePersonInfo.setOrganizationName(SecurityUtils.getOrgName());
            //把前端传过来的username转成userid传入
            enterprisePersonInfo.setCreateId(sysUserMapper.selectUserByUserName(enterprisePersonInfo.getUserName()).getUserId());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return enterprisePersonInfoMapper.insertEnterprisePersonInfo(enterprisePersonInfo);
    }

    /**
     * 修改企业人员管理
     * 
     * @param enterprisePersonInfo 企业人员管理
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateEnterprisePersonInfo(EnterprisePersonInfo enterprisePersonInfo)
    {

          enterprisePersonInfo.setOperateTime(new Timestamp(new Date().getTime()));
          enterprisePersonInfo.setOrganizationId(SecurityUtils.getOrgId());
          //查询是否存在该电话
        List<EnterprisePersonInfo> enterprisePersonInfos = enterprisePersonInfoMapper.selectEnterprisePersonInfoListByPhone(enterprisePersonInfo);
        if (enterprisePersonInfos.size()>0 && !enterprisePersonInfo.getId().equals(enterprisePersonInfos.get(0).getId())){
                //不相同，说明存在别的电话号码
                throw new ServiceException("电话号码不可重复");
            }
            //把前端传过来的username转成userid传入
            enterprisePersonInfo.setCreateId(sysUserMapper.selectUserByUserName(enterprisePersonInfo.getUserName()).getUserId());
        return enterprisePersonInfoMapper.updateEnterprisePersonInfo(enterprisePersonInfo);
    }

    /**
     * 批量删除企业人员管理
     * 
     * @param ids 需要删除的企业人员管理主键
     * @return 结果
     */
    @Override
    public int deleteEnterprisePersonInfoByIds(String[] ids)
    {
        return enterprisePersonInfoMapper.deleteEnterprisePersonInfoByIds(ids);
    }

    /**
     * 删除企业人员管理信息
     * 
     * @param id 企业人员管理主键
     * @return 结果
     */
    @Override
    public int deleteEnterprisePersonInfoById(String id)
    {
        return enterprisePersonInfoMapper.deleteEnterprisePersonInfoById(id);
    }

    @Override
    public List<EnterprisePersonInfo> selectEnterprisePersonInfoListByPhone(EnterprisePersonInfo enterprisePersonInfo) {
        return enterprisePersonInfoMapper.selectEnterprisePersonInfoListByPhone(enterprisePersonInfo);
    }
}
